function analyze_initialization_results(results_filename)
%% Results will probably be kept in <thermalvis/nodes/monoslam/log> folder

% e.g. /home/steve/ros_laboratory/thermalvis-ros-pkg/thermalvis/nodes/monoslam/log/1345606640.383751323.txt
R = load(results_filename);

s1 = [];
s2 = [];

% Categorize results
for iii = 1:size(R,1)
   if (R(iii,9) == 1)
      s1 = [s1; R(iii,3:7)]; 
   elseif (R(iii,9) == 0)
      s2 = [s2; R(iii,3:7)]; 
   end
end

size(s1)
size(s2)

figure(1)
hold on
plot(s1(:,4), 1, ' bo');
plot(s2(:,4), 0, ' rx');
hold off

labels = {'twoErr', 'gricScore', 'infrontScore', 'tScore', 'dScore'};
bestMeans = zeros(1,size(s1,2));

% For each variable
for iii = 1:size(s1,2)
    
    bestMeans(iii) = 0;

    for jjj = 1:size(s1,1)
        bestMeans(iii) = bestMeans(iii) + s1(jjj,iii);
    end

    bestMeans(iii) = bestMeans(iii) / size(s1,1);
   
   % Prepare SVM data
   
   xapp = [];
   yapp = [];
   
   for jjj = 1:size(s1,1)
        xapp = [xapp; s1(jjj,iii)];
        yapp = [yapp; 1];
   end
   
   for jjj = 1:size(s2,1)
       if (s1(jjj,iii) >= 0)
           xapp = [xapp; s1(jjj,iii)];
           yapp = [yapp; -1];
       end
   end
    
    xapp
    yapp
    
    H = figure;
    c = inf;
    epsilon = .000001;
    kerneloption= 1; % kerneloption= 1;
    kernel='poly'; % kernel='gaussian'; 'poly'
    verbose = 1;
    tic
    [xsup,w,b,pos,timeps,alpha,obj]=svmclass(xapp,yapp,c,epsilon,kernel,kerneloption,verbose);
    toc
    
    marker_size = 8;
    
    
    
    %--------------Testing Generalization performance ---------------
    minX = (absMinS - 0.1*absRangeS);
    minY = (absMinF - 0.1*absRangeF);
    maxX = (absMaxS + 0.1*absRangeS);
    maxY = (absMaxF + 0.1*absRangeF);
    
    [xtesta1,xtesta2]=meshgrid([minX:(maxX-minX)/500:maxX],[minY:(maxY-minY)/500:maxY]);
    [na,nb]=size(xtesta1);
    xtest1=reshape(xtesta1,1,na*nb);
    xtest2=reshape(xtesta2,1,na*nb);
    xtest=[xtest1;xtest2]';
    ypred = svmval(xtest,xsup,w,b,kernel,kerneloption);
    ypredmat=reshape(ypred,na,nb);
    R = input('continue?')
   
end

labels
bestMeans


worstLowMeans = zeros(1,size(s2,2));

worstHiMeans = zeros(1,size(s2,2));

% For each variable
for iii = 1:size(s2,2)
    
    wLC = 0;
    wHC = 0;
    
   for jjj = 1:size(s2,1)
       
       if (s2(jjj,iii) < bestMeans(iii))
           wLC = wLC + 1;
           worstLowMeans = worstLowMeans + s2(jjj,iii);
       else
           wHC = wHC + 1;
           worstHiMeans = worstHiMeans + s2(jjj,iii);
       end
       
   end
   
   worstLowMeans(iii) = worstLowMeans(iii) / wLC;
   worstHiMeans(iii) = worstHiMeans(iii) / wHC;
   
end

%worstLowMeans
%worstHiMeans

end